# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=atuin
pkgver=0.10.0
pkgrel=0
# Keep in sync with sqlx version in Cargo.lock.
_sqlx_ver=0.5.13
pkgdesc="Magical shell history"
url="https://github.com/ellie/atuin"
# armhf: build getting stuck on CI
# riscv64, s390x: blocked by rust/cargo
arch="all !armhf !riscv64 !s390x"
license="MIT"
makedepends="
	cargo
	clang
	jq
	libsodium-dev
	openssl-dev
	sqlite-dev
	"
pkgusers="atuin"
install="
	$pkgname-server.pre-install
	$pkgname.post-install"
subpackages="
	$pkgname-sync
	$pkgname-server
	$pkgname-server-openrc
	$pkgname-zsh-plugin:_zsh_plugin:noarch
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="https://github.com/ellie/atuin/archive/v$pkgver/atuin-$pkgver.tar.gz
	https://github.com/launchbadge/sqlx/archive/v$_sqlx_ver/sqlx-$_sqlx_ver.tar.gz
	zsh-dont-bind-up-arrow.patch
	use-system-tls.patch
	sqlx-use-system-libsqlite.patch
	server-log-without-ansi-style.patch
	cargo-lock.patch

	$pkgname.initd
	$pkgname.confd
	$pkgname.logrotate
	"
options="!check"  # FIXME: some tests are broken

export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
export CARGO_PROFILE_RELEASE_LTO="true"
export CARGO_PROFILE_RELEASE_OPT_LEVEL="s"
export CARGO_PROFILE_RELEASE_PANIC="abort"

export SODIUM_USE_PKG_CONFIG=1

prepare() {
	local sqlx_ver
	sqlx_ver=$(cargo metadata --format-version 1 \
		| jq -er '.packages[] | select(.name=="sqlx") | .version') \
		|| { error 'Unable to find sqlx version in Cargo.lock'; return 1; }
	if [ "$_sqlx_ver" != "$sqlx_ver" ]; then
		error "Bump _sqlx_ver in APKBUILD to: $sqlx_ver"
		return 1
	fi

	mkdir -p vendor
	mv "$srcdir"/sqlx-$_sqlx_ver vendor/sqlx

	# Build with sqlx which we patched to remove bundled libsqlite3.
	# See https://github.com/launchbadge/sqlx/issues/191.
	cat >> Cargo.toml <<-EOF

		[patch.crates-io]   
		sqlx = { path = "./vendor/sqlx" }
		sqlx-core = { path = "./vendor/sqlx/sqlx-core" }
	EOF

	default_prepare

	cargo fetch --locked
}

build() {
	msg2 'Building server'
	cargo build --frozen --release --no-default-features --features server
	mv target/release/atuin target/release/atuin-server

	msg2 'Building client with sync'
	cargo build --frozen --release --no-default-features --features client,sync
	mv target/release/atuin target/release/atuin-sync

	msg2 'Building client without sync'
	cargo build --frozen --release --no-default-features --features client

	mkdir -p comp
	local shell; for shell in bash fish zsh; do
		./target/release/atuin gen-completions -s $shell > comp/atuin.$shell
	done
}

check() {
	cargo test --workspace --frozen --features client,sync,server
}

package() {
	pkgdesc="$pkgdesc - offline only client"
	provider_priority=100  # highest

	install -D -m755 target/release/atuin -t "$pkgdir"/usr/bin/
	install -D -m755 target/release/atuin-server -t "$pkgdir"/usr/bin/

	install -D -m644 comp/atuin.bash "$pkgdir"/usr/share/bash-completion/completions/atuin
	install -D -m644 comp/atuin.fish "$pkgdir"/usr/share/fish/completions/atuin.fish
	install -D -m644 comp/atuin.zsh "$pkgdir"/usr/share/zsh/site-functions/_atuin

	install -d -m755 -o atuin "$pkgdir"/etc/$pkgname
	install -D -m640 -o atuin atuin-server/server.toml -t "$pkgdir"/etc/$pkgname/

	install -D -m755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -D -m644 "$srcdir"/$pkgname.logrotate "$pkgdir"/etc/logrotate.d/$pkgname
}

sync() {
	pkgdesc="$pkgdesc - client with history sync"
	provides="$pkgname=$pkgver-r$pkgrel"
	provider_priority=10  # lowest

	install -D -m755 "$builddir"/target/release/atuin-sync "$subpkgdir"/usr/bin/atuin
}

server() {
	pkgdesc="$pkgdesc - sync server"

	amove etc/atuin/server.toml
	amove usr/bin/atuin-server
}

openrc() {
	default_openrc
	install_if="openrc $pkgname-server=$pkgver-r$pkgrel"
}

_zsh_plugin() {
	pkgdesc="$pkgdesc (Zsh plugin)"
	depends="atuin=$pkgver-r$pkgrel"

	install -D -m644 "$builddir"/src/shell/atuin.zsh \
		"$subpkgdir"/usr/share/zsh/plugins/$pkgname/$pkgname.plugin.zsh
}

sha512sums="
6724de645b77e57f3aca33cb1af2637f85b25662d15dc7354117928080ec5c77d7ef615c61543db6bda206e1e1330022d5685a584fed1fc8712fcbd719078678  atuin-0.10.0.tar.gz
756df570c14d00948d5e94f841e3bc82b1be6b3809b7108870cc69247fc1a7bf09ccb3f9a44eddc8254a74b7aace26951d65e786a73274b9379714e0e68d5289  sqlx-0.5.13.tar.gz
3f34e79c81d3f29df7bd0bcb06fee8c3cf325a7e96bdfb0b5080cc1078972ce2092795a6918b277483221c7d29dd99221bd32012172f87ee3bde5404315ade64  zsh-dont-bind-up-arrow.patch
46527e0b5b76022fe7259338ec61f3fe2f4c1803c7fc67274a814e7d2ad5dfa9ef5c878f4bcc0b7da1184fd381c4127b48cb27eead91af1d49d5dbe242462f42  use-system-tls.patch
334f326dbeb9382b2d5d461afe9fd41fc043f4d0432712f484829fc1a697d6aa36d73d3dbafa3add126ee29a562aef005010fd806d17e439eac800aac763617e  sqlx-use-system-libsqlite.patch
8e78e6a562ae310ef40cf62e0bd7dddbca5611e9321df551e07f21292acf3e925568218be17b8afaf4c1a1c104c5210829c1fa0334904227af45043f23db531e  server-log-without-ansi-style.patch
c2a71c6e9cba51ec416916a521ed65ebd002a0cde822a0e42281e59f456958815a7bdf6041c3a636ad0a84910e08c2e1508390c1be4b99b4d374ddb18c57801b  cargo-lock.patch
c58622c6e3ab16a5d84204f0ef313c5dd6dc912da7295531983e2497d9e7914765c8e1bbe8d67fd5fe47f577c5be792830fb22cd59973f5f796b88c55b5e0534  atuin.initd
5f1631f0b06f19733025ad3efec6b44ef13937cd8f2f9b7e3cc8c0ba53495850c06fb8c5377d1d003dda00e2d11835056d3fc1b3ba8ecefb575e61a83159f33e  atuin.confd
30946fe22ede072d76331762c862382f03a35735e5741c46367320a116ac285615ae757e20a9cf7adceb8ba24b827581992fa5b7f84e595cd862a3c2ae69dbe0  atuin.logrotate
"
